package com.demos.diveinspring.bootstrap;

import com.demos.diveinspring.condition.ConditionalOnSystemProperty;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

//@ConditionalOnSystemProperty(name = "user.name", value = "Mercy")
public class ConditionalOnSystemPropertyBootstrap {


    @Bean
    @ConditionalOnSystemProperty(name = "user.name", value = "Tom Xie")
    public String helloWorld() {
        return "Hello ConditionalOnSystemProperty, Tom!!!!!!";
    }

    public static void main(String[] args) {
        // 当ConditionalOnSystemPropertyBootstrap.class被当做参数传入的时候，这个ConditionalOnSystemPropertyBootstrap
        // 就会被当做一个bean扫描，所以可以不用加@ConditionalOnSystemProperty
        ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class)
                .web(WebApplicationType.NONE)
                .run(args);
        String helloWorld = context.getBean("helloWorld", String.class);
        System.out.println("ConditionalOnSystemProperty helloWorld bean: " + helloWorld);

        context.close();
    }
}
